# -*- coding: utf-8 -*-
import scrapy
from scrapy.spiders import Rule
from scrapy.linkextractors import LinkExtractor
import re
from douban_movie.items import MovieItem
from scrapy.exceptions import CloseSpider


class AwesomeMovieSpider(scrapy.spiders.CrawlSpider):
    name = 'awesome-movie'
    allowed_domains = ['movie.douban.com']
    start_urls = ['http://movie.douban.com/subject/3011091']
    link = LinkExtractor(allow="movie.douban.com/subject/\d+",restrict_css='div#recommendations dd a')
    rules = (
      Rule(link_extractor=link,callback='parse_page',follow=True),
    )
    num = 0


    def parse_movie_item(self, response):
        "TODO: 解析 item"
        
        score = response.xpath('//strong[contains(@class, "rating_num")]/text()').extract_first()
        print(score)
        if float(score) < 8.0:
            return
        item = MovieItem()
        item['url'] = response.url
        item['name'] = response.xpath('//div[@id="content"]/h1/span[1]/text()').extract_first()
        summary =  response.xpath('//div[@id="link-report"]/span/span/text()').extract_first()
        if(summary == None):
            summary =  response.xpath('//div[@id="link-report"]/span/text()').extract_first()
        item['summary'] = summary
        item['score'] = score
        self.num = self.num + 1
        if self.num > 40:
            raise CloseSpider('强制停止')
        return item

    def parse_start_url(self, response):
        yield self.parse_movie_item(response)


    def parse_page(self, response):
        yield self.parse_movie_item(response)